﻿Imports Microsoft.VisualBasic
Imports System.Data

<System.ComponentModel.DataObjectAttribute(True)> _
Public Class BLL_AddEnlistment

    Public Sub New()
        ' 
        ' TODO: 在此加入建構函式的程式碼 
        ' 
    End Sub

    'SELECT 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.[Select])> _
    Public Function BLL_Select(ByVal EnlistID As Integer) As DataTable

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("SELECT * FROM EnlistHeader WHERE EnlistID = {0} ", EnlistID)
        Return dat.GetData(sql)

    End Function

    'Insert 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Insert)> _
    Public Function BLL_Insert(ByVal EnlistName As String, ByVal WorkType As String, ByVal ApplyStartDate As DateTime, ByVal ApplyEndDate As DateTime, ByVal StartDate As DateTime, ByVal EndDate As DateTime, ByVal UOrganizationID As String, ByVal PeopleRequired As Integer, ByVal Location As String, ByVal Description As String, ByVal Owner As String, ByVal PhoneNumber As String, ByVal FaxNumber As String, ByVal Email As String, ByVal CreateBy As String) As Integer

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            sql.AppendFormat("INSERT EnlistHeader (EnlistName,WorkType,ApplyStartDate,ApplyEndDate,StartDate,EndDate,UOrganizationID,PeopleRequired,Location,Description,Owner,PhoneNumber,FaxNumber,Email,CreateDate,CreateBy) ")
            sql.AppendFormat("SELECT '{0}'", EnlistName)
            sql.AppendFormat(", '{0}'", WorkType)
            sql.AppendFormat(", '{0:yyyy/MM/dd}'", ApplyStartDate)
            sql.AppendFormat(", '{0:yyyy/MM/dd}'", ApplyEndDate)
            sql.AppendFormat(", '{0:yyyy/MM/dd}'", StartDate)
            sql.AppendFormat(", '{0:yyyy/MM/dd}'", EndDate)
            sql.AppendFormat(", '{0}'", UOrganizationID)
            sql.AppendFormat(", {0}", PeopleRequired)
            sql.AppendFormat(", '{0}'", Location)
            sql.AppendFormat(", '{0}'", Description)
            sql.AppendFormat(", '{0}'", Owner)
            sql.AppendFormat(", '{0}'", PhoneNumber)
            sql.AppendFormat(", '{0}'", FaxNumber)
            sql.AppendFormat(", '{0}'", Email)
            sql.AppendFormat(", getdate()")
            sql.AppendFormat(", '{0}'; ", CreateBy)

            '-----------------------
            '更改EnlistTrainingCourse
            '-----------------------
            sql.AppendFormat("UPDATE EnlistTrainingCourse ")
            sql.AppendFormat("SET EnlistID=@@IDENTITY ")
            sql.AppendFormat("WHERE EnlistID=0 AND CreateBy='{0}'; ", CreateBy)

            dat.AccData(sql)
            scope.Complete()

            Return 1

        End Using

    End Function

    'Update 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)> _
    Public Function BLL_Update(ByVal EnlistName As String, ByVal WorkType As String, ByVal ApplyStartDate As DateTime, ByVal ApplyEndDate As DateTime, ByVal StartDate As DateTime, ByVal EndDate As DateTime, ByVal UOrganizationID As String, ByVal PeopleRequired As Integer, ByVal Location As String, ByVal Description As String, ByVal Owner As String, ByVal PhoneNumber As String, ByVal FaxNumber As String, ByVal Email As String, ByVal UpdateBy As String, ByVal original_EnlistID As Integer) As Integer

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            sql.AppendFormat("UPDATE EnlistHeader ")
            sql.AppendFormat("SET EnlistName='{0}'", EnlistName)
            sql.AppendFormat(",WorkType='{0}'", WorkType)
            sql.AppendFormat(",ApplyStartDate='{0:yyyy/MM/dd}'", ApplyStartDate)
            sql.AppendFormat(",ApplyEndDate='{0:yyyy/MM/dd}'", ApplyEndDate)
            sql.AppendFormat(",StartDate='{0:yyyy/MM/dd}'", StartDate)
            sql.AppendFormat(",EndDate='{0:yyyy/MM/dd}'", EndDate)
            sql.AppendFormat(",UOrganizationID='{0}'", UOrganizationID)
            sql.AppendFormat(",PeopleRequired={0}", PeopleRequired)
            sql.AppendFormat(",Location='{0}'", Location)
            sql.AppendFormat(",Description='{0}'", Description)
            sql.AppendFormat(",Owner='{0}'", Owner)
            sql.AppendFormat(",PhoneNumber='{0}'", PhoneNumber)
            sql.AppendFormat(",FaxNumber='{0}'", FaxNumber)
            sql.AppendFormat(",Email='{0}'", Email)
            sql.AppendFormat(",UpdateDate=getdate()")
            sql.AppendFormat(",UpdateBy='{0}'", UpdateBy)
            sql.AppendFormat("WHERE EnlistID={0}; ", original_EnlistID)

            '-----------------------
            '更改EnlistTrainingCourse
            '-----------------------

            sql.AppendFormat("UPDATE EnlistTrainingCourse ")
            sql.AppendFormat("SET EnlistID='{0}' ", original_EnlistID)
            sql.AppendFormat("WHERE EnlistID=0 AND CreateBy='{0}'; ", UpdateBy)

            dat.AccData(sql)

            scope.Complete()

            Return 1

        End Using

    End Function

    'Delete 
    <System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Delete)> _
    Public Sub BLL_Delete(ByVal EnlistID As Integer)

        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        Using scope As New Transactions.TransactionScope

            sql.Length = 0
            sql.AppendFormat("DELETE EnlistHeader WHERE EnlistID={0};", EnlistID)
            sql.AppendFormat("DELETE EnlistTrainingCourse WHERE EnlistID={0};", EnlistID)
            dat.AccData(sql)

            scope.Complete()

        End Using

    End Sub

End Class
